home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / (Notes) / Grayscale Information.doc < prev    next >
Text File  |  1993-12-15  |  12KB  |  227 lines

  1. Note from Apple.
  2. Grayscale Information
  3.  
  4. Article Created: 23 April 1989
  5. Article Last Reviewed: 6 July 1992
  6. Article Last Updated:
  7.  
  8. TOPIC -----------------------------------------------
  9.  
  10. A user has found (using very sophisticated photometric devices) that when
  11. he generates a monochrome palette from a color palette, some of the tones
  12. are identical.  This article discusses this phenomenon.
  13.  
  14. DISCUSSION ------------------------------------------
  15.  
  16. In regard to using the color palette, the Palette Manager does not place
  17. new colors into the display device's color look-up table (CLUT).  It maps
  18. the requested colors in a palette to available colors within the current
  19. color table.  The correct method for using specific color values is to use
  20. the Color Manager calls to create and load a programmed color table.
  21.  
  22. There are some other points that must taken into account after setting
  23. your colors with the Color Manager.
  24.  
  25. - It is possible to produce grays by setting the monitor to monochrome
  26. output and not setting grays, as the user in question probably does.  When
  27. selecting monochrome from the monitor's Control Panel CDEV, luminance
  28. mapping of the colors in the color table is done to calculate and replace
  29. the RGB values for a monochrome pixel.  The luminance mapping follows this
  30. equation:
  31.  
  32.    Luminance = .3*red + .59*green + .11*blue
  33.  
  34. The resulting value is placed into the display device's table for R, G,
  35. and B.  The values are altered according to the above equation. This needs
  36. to be taken into account if the monochrome option is chosen for the Apple
  37. High-Resolution Color Monitor.
  38.  
  39. - If you place your own RGB "gray" values into a color table, you must
  40. also take into account gamma correction. Gamma correction is done by the
  41. display device's driver to correct the disproportionate light output of
  42. the CRT phosphors by comparison with the proportional input voltages of
  43. the video signal.  To set a linear relationship between colors and output,
  44. the values set for the Macintosh II Video Card are changed to new values,
  45. which then produce the corrected color output on the monitor. Test
  46. equipment detects a gamma-filtered set of values from the monitor.
  47.  
  48. The gamma correction is automatically handled for all values written to
  49. the device's color table.  This is based on an empirically-derived gamma
  50. look-up table. If the selected values fall within the same look-up table
  51. reference, they can be mapped to the same gamma value.  This would explain
  52. why some values are the same.   The gamma table ID is located in the SCRN
  53. resource of the System file for the Macintosh II Video Card. A new GAMA
  54. Resource table can be defined and installed in the System file for use by
  55. the Macintosh II Video Card. To use the new gamma table, the GAMA resource
  56. ID must be changed in the SCRN resource to that of the new table.
  57.  
  58. The safest values for any testing will probably be a linear table that
  59. does no gamma correction.  A quick and easy way to do this is to pass -1
  60. [dgp: according to Designing Cards & Drivers you should pass NULL, not -1] to the SetGamma Control parameter block as the new table address.  This
  61. causes a linear table to be used, guaranteeing that all of the possible
  62. gray levels of the card are available.  At that point, the actual color
  63. output on the monitor depends on the phosphors themselves, which have a
  64. variance from monitor to monitor of +/-.02 for the red and green phosphors
  65. and +/-.015 for the blue. The CIE coordinates of the phosphors are:
  66.  
  67.    Red: x=.625 y=.34 Green: x=.28   y=.595 Blue:     x=.155 y=.070
  68.  
  69. To properly set the gamma table, see the information below about how the
  70. driver uses this table.  You should also investigate how to control device
  71. drivers. This is described in "Inside Macintosh Volume II." Another source
  72. of information is the "Designing Cards and Drivers Manual for the
  73. Macintosh."
  74.  
  75. Gamma Information
  76. -----------------
  77. In the current implementation of the video drivers, gamma correction is
  78. applied to requested, absolute colors, immediately before they are set in
  79. the color look-up table by the SetEntries control call. More specifically,
  80. some number of high-order bits are extracted from the red, green, and blue
  81. channels and used as an index into tables of corrected values.  These
  82. values are then placed into the hardware, yielding corrected output. On
  83. the Macintosh II Video Card (the TFB card), the high eight bits of each
  84. channel are used to reference the gamma table.
  85.  
  86. There are a number of minor shortcomings in this implementation. First,
  87. there is not absolute symmetry between the SetEntries control call, which
  88. sets the CLUT, and the GetEntries status call, which reads the CLUT
  89. hardware, because the gamma correction took place as part of the
  90. SetEntries call. Also, the uncorrected values are generally unrecoverable
  91. (although a copy of the absolute colors are always available in the
  92. GDevice structure).
  93.  
  94. Finally, it is most desirable to extract more bits as an index to the
  95. gamma table than the number of bits of color information that will be set
  96. in the CLUT.  This way, you avoid a loss of color resolution after
  97. correction. For example, the TFB card has an eight-bit-per-channel CLUT,
  98. but only uses the most-significant eight bits of the (16-bit) channel
  99. information for gamma lookup. At lower intensities, the gamma correction
  100. increases the distance between adjacent values.  As a result, on the TFB
  101. card, some dynamic range is lost at lower intensities. This could be
  102. corrected by extracting nine or ten bits of channel information rather
  103. than eight and using a larger, gamma-correction table, but this option was
  104. declined to reduce gamma table size.
  105.  
  106. The GammaTbl Data Structure
  107. ---------------------------
  108. [dgp: This section has many errors. I suggest you turn instead to Designing Cards and Drivers, 3rd Ed., which, finally, includes this information.]
  109.  
  110. The structure itself has been a bit of a mystery, as it is not defined in
  111. either "Inside Macintosh Volume V" or the Designing Cards and Drivers
  112. Manual. This is the structure:
  113.  
  114.    record GammaTable of
  115.    gVersion: integer; {gtab version, currently 0}
  116.    gType: integer; {drHwId value}
  117.    gFormulaSize: integer; {size of formula data, below}
  118.    gChanCnt: integer; {# of component channels}
  119.    gDataCnt: integer; {# of values per channel}
  120.    gDataWidth: integer; {size of data in tables}
  121.    {gamma correction look-up tables}
  122.    gFormulaData: array [0.. gFormulaSize] of byte;
  123.    {data for gamma calculation formula}
  124.    gData: array [0.. gDataCnt] of byte;
  125.    end;
  126.  
  127. In this structure, gVersion represents the gamma table format version,
  128. which is 0 for all current cards.  The gType field holds the drHwId value
  129. for this board to identify the board that this table was measured for.
  130. Note that this means that a single gamma table can't directly be shared
  131. between two different cards, even if they both have the same CLUT response
  132. curve (which is usually linear).  This lets the data in the gamma table be
  133. in an appropriate form for varying hardware (that is, a card could have
  134. four-bit/channel DACs and might prefer gamma data in the range $0..$F
  135. rather than $0..$FF).
  136.  
  137. gFormulaSize defines the number of bytes occupied by the gFormulaData
  138. field. On Apple's current video cards, gamma correction is done by
  139. modifying the values loaded into the CLUT by the SetEntries control call
  140. to approximate linear response on the display.  On these systems, the
  141. gamma correction table acts as a final, look-up, data table, which
  142. translates the requested color into closest available linearized level.
  143. These gamma table values are determined empirically by measuring the
  144. output of a calibrated display.
  145.  
  146. More sophisticated systems may choose an alternative to this simple
  147. look-up mechanism.  For instance, you can calculate gamma correction
  148. factors based on a mathematical response function. By default, the TFB
  149. card uses a single correction table for all three channels. No
  150. calculations are performed on the incoming color table other than simple
  151. lookup.  Cards can remember the specific monitor configuration at the
  152. beginning of the gFormulaData field, allowing it to identify and use only
  153. gamma tables developed for the currently connected monitor.
  154.  
  155. gChanCnt is the number of look-up tables in gData, below. If there is more
  156. than one channel of gamma correction data, then the R, G, and B tables
  157. follow each other, respectively, at the end of the structure.
  158.  
  159. gDataCnt is the number of discrete look-up values included in each of the
  160. channel's correction table.  It is always equal to 2gDataWidth [dgp: I think they meant to say 1<<gDataWidth], but refers to number of bytes that this channel's data occupies. 
  161.  
  162. gDataWidth describes the number of significant bits of information
  163. available in each entry in a channel's correction table. The data always
  164. appears as gDataWidth bits, right-justified in a field that is the next
  165. larger number of bytes than gDataWidth.  Because it is rare to have
  166. devices with more than eight bits of CLUT resolution, virtually all
  167. devices pack their correction data into bytes.
  168.  
  169. gData is actual correction table data. If there is more than one channel's
  170. information, each table follows the next in R, G, B order. The standard
  171. tables included in Apple's driver have only one table, which is applied to
  172. all three output channels.  Because Pascal cannot express variable size
  173. fields in record structures, the independent channels are not individually
  174. named.
  175.  
  176. "gama" Resource Format
  177. ----------------------
  178. In addition to the RAM data structure for gamma tables covered above,
  179. there is a standard resource format for gamma table resources.  Like many
  180. other resource templates, the gamma structure is an image of the RAM form
  181. stored in resource format. There are no changes.
  182.  
  183. Using Gamma Correction
  184. ----------------------
  185. Gamma correction is always applied by the TFB video driver. At driver open
  186. time, the driver is usually initialized with a linear (noncorrecting)
  187. gamma table.  When _InitGraf is called, the "scrn" screen configuration
  188. resource is read from the System file. This resource (described in "Inside
  189. Macintosh Volume V") includes information about the size and orientation
  190. of the different monitors configured into the system, including their last
  191. video mode (pixelsize), color table, and gamma table.
  192.  
  193. If there is no "gama" resource ID specified, or the specified ID is not
  194. present, then a default gamma table, "gama" =0 is loaded from the System
  195. file and used (this is the table calculated for the TFB card). If the
  196. specified resource is found, then the appropriate resource is loaded, and
  197. a control call is issued to the driver to make this the current gamma
  198. table. Unfortunately, there is currently no tool to set the "gama" ID,
  199. short of modifying the "scrn" resource directly.
  200.  
  201. To facilitate the use of the gamma table, there are two calls in the
  202. standard, video-driver routines that set the gamma table (control call 4,
  203. SetGamma) and retrieve the pointer to the current gamma table (status call
  204. 6 [dgp: oops, they mean 8] on TFB rev 2 drivers and up). These calls simply take and return a pointer to a GammaTbl structure.
  205.  
  206.  
  207. Copyright 1991 Apple Computer, Inc.
  208.  
  209.  
  210. ******************************************************************
  211. Written & sent to Apple: July 24, 1989
  212. Revised: January 7, 1993
  213.  
  214. I am writing to report some errors in the otherwise helpful document:
  215. "Grayscale Information" (Rev. 23 April '89) in Tech Info.
  216.  
  217. 0. "A quick and easy way to do this is to pass -1 to the SetGamma Control parameter block as the new table address."  According to Designing Cards & Drivers, 3rd Ed., we should pass NULL, not -1.
  218.  
  219. 1. In the next to last line, it says that GetGamma is "(status call 6 on TFB rev 2 drivers and up)". This is wrong. The MPW header file Video.h correctly defines cscGetGamma = 8. 
  220.  
  221. 2. The explanation of gDataCnt is wrong. "It is always equal to 2gDataWidth..." Presumably it was meant to say 2^gDataWidth or 1<<gDataWidth. However, is this really true? There is a video card on the market now with 9-bit dacs. Does it conform to this rule?
  222.  
  223. 3. Another error in the same paragraph: "The data always appears ... in a field that is the next larger number of bytes than gDataWidth." According to this sentence, since gDataWidth is 8 bits, the data would be stored in two bytes, not one. What is intended is "larger or equal to", or to rephrase, "the smallest number of bytes that will hold gDataWidth bits".
  224.  
  225. Denis G. Pelli
  226. Institute for Sensory Research
  227. Syracuse University